package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.MaintenancePlanDao;
import com.kun.portal.webframe.dao.RepairPlaneDao;
import com.kun.portal.webframe.po.MaintenancePlanPo;
import com.kun.portal.webframe.po.RepairPlanePo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;

/**
 * Created by Lenovo on 2018/7/19.
 */
@Service
public class RepairPlaneService {
    @Autowired
    private RepairPlaneDao repairPlaneDao;
    @Autowired
    private EntityManager entityManager;

    public Page<RepairPlanePo> find(String filter, String status, Boolean isFlight, Date startDate , Date endDate, Pageable pageable){
        if(StringUtils.isNotBlank(filter)||StringUtils.isNotBlank(status)||null!=isFlight||startDate!=null||endDate!=null){
            Specification<RepairPlanePo> specification =  new Specification<RepairPlanePo>() {
                @Override
                public Predicate toPredicate(Root<RepairPlanePo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Predicate predicate1 = null;
                    if (StringUtils.isNotBlank(filter)){
                        Predicate p1=cb.like(root.get("equipmentPo").get("equipmentName"), "%"+filter+"%");
                        Predicate p2=cb.like(root.get("equipmentPo").get("equipmentNum"), "%"+filter+"%");
                        Predicate p3 = cb.like(root.get("flightInfoPo").get("flightNum"),"%"+filter+"%");
                        predicate1 = cb.or(p1,p2,p3);
                    }
                    Predicate predicate2 = null;
                    if(null!=isFlight){
                        predicate2 = cb.equal(root.get("flight"),isFlight);
                    }
                    Predicate predicate3 = null;
                    if(startDate!=null&&endDate!=null){

                        if(startDate!=null&&endDate!=null){
                            predicate3 = cb.between(root.get("planeDate"),startDate,endDate);
                        }else{
                            if(startDate!=null){
                                predicate3 = cb.greaterThanOrEqualTo(root.get("planeDate"),startDate);
                            }else {
                                predicate3 = cb.lessThanOrEqualTo(root.get("planeDate"),endDate);
                            }
                        }
                    }
                    Predicate predicate4=null;
                    if(StringUtils.isNotBlank(status)){
                        predicate4 = cb.equal(root.get("status"),status);
                    }
                    Predicate predicate=null ;
                    if(predicate2!=null){
                        predicate=predicate2;
                    }
                    if(predicate3!=null){
                        if(predicate==null){
                            predicate = predicate3;
                        }else{
                            predicate = cb.and(predicate,predicate3);
                        }
                    }
                    if(predicate4!=null){
                        if(predicate==null){
                            predicate = predicate4;
                        }else{
                            predicate = cb.and(predicate,predicate4);
                        }

                    }
                    if(predicate1!=null){
                        if(predicate!=null){
                            Predicate predicate5 =query.where(predicate,predicate1).getRestriction();
                        }else{
                            predicate = predicate1;
                        }
                    }
                    return predicate;
                }
            };
            return repairPlaneDao.findAll(specification,pageable);

        }
        return repairPlaneDao.findAll(pageable);
    }


}
